﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellTalkSessionContext.h"
#include "CellInetAddress.h"
#include "CellCloudGraph.h"
#include "CellPacket.h"
#include "CellPrimitiveSerializer.h"
#include "CellPrimitive.h"
#include "CellTalkPacketDefinition.h"
#include "CellEndpoint.h"

namespace cell
{

//-----------------------------------------------------------------------
TalkTracker::TalkTracker(const String& tag, const InetAddress& address)
	: mTag(tag)
	, mEndpoint(NULL)
	, activeCellet(NULL)
{
	mEndpoint = CELL_NEW Endpoint(tag, NucleusConfig::CONSUMER, address, 0);
}
//-----------------------------------------------------------------------
TalkTracker::~TalkTracker(void)
{
	CELL_DELETE mEndpoint;
	mEndpoint = NULL;
}
//-----------------------------------------------------------------------
const String& TalkTracker::getTag(void) const
{
	return mTag;
}
//-----------------------------------------------------------------------
Endpoint* TalkTracker::getEndpoint(void) const
{
	return mEndpoint;
}



//-----------------------------------------------------------------------
TalkSessionContext::TalkSessionContext(void)
	: tickTime(0)
	, proxy(false)
	, expiredTime(0)
{
}
//-----------------------------------------------------------------------
TalkSessionContext::~TalkSessionContext(void)
{
	Trackers::iterator iter, end = mTrackers.end();
	for (iter = mTrackers.begin(); iter != end; ++iter)
	{
		CELL_DELETE (iter->second);
	}
	mTrackers.clear();
}
//-----------------------------------------------------------------------
TalkTracker* TalkSessionContext::getTracker(const String& tag)
{
	CELL_LOCK_MUTEX(mMutex);

	Trackers::iterator iter = mTrackers.find(tag);
	if (iter != mTrackers.end())
	{
		return iter->second;
	}

	return NULL;
}
//-----------------------------------------------------------------------
TalkTracker* TalkSessionContext::addTracker(const String& tag, const InetAddress& address)
{
	CELL_LOCK_MUTEX(mMutex);

	Trackers::iterator iter = mTrackers.find(tag);
	if (iter != mTrackers.end())
	{
		return iter->second;
	}

	TalkTracker* ider = CELL_NEW TalkTracker(tag, address);
	mTrackers.insert(Trackers::value_type(tag, ider));
	return ider;
}
//-----------------------------------------------------------------------
void TalkSessionContext::removeTracker(const String& tag)
{
	CELL_LOCK_MUTEX(mMutex);

	Trackers::iterator iter = mTrackers.find(tag);
	if (iter != mTrackers.end())
	{
		TalkTracker* ider = iter->second;
		CELL_DELETE ider;
		mTrackers.erase(iter);
	}
}
//-----------------------------------------------------------------------
void TalkSessionContext::listTrackerTags(StringVector& list)
{
	CELL_LOCK_MUTEX(mMutex);

	Trackers::const_iterator iter, end = mTrackers.end();
	for (iter = mTrackers.begin(); iter != end; ++iter)
	{
		list.push_back(iter->first);
	}
}
//-----------------------------------------------------------------------
uint TalkSessionContext::packetDialogue(char* out, Primitive& primitive)
{
	if (primitive.isEmpty())
	{
		return 0;
	}

	// 序列化原语
	SerializerStream pstream;
	primitive.write(pstream);
	String primitiveStream;
	pstream.str(primitiveStream);

	// 序列化后的原语入包
	char packetTag[5] = {0x0};
	TPT_DIALOGUE_ASSIGN(packetTag);
	Packet packet(packetTag, 99);
	packet.setBody(primitiveStream.c_str(), primitiveStream.length());

	// 数据打包
	uint length = Packet::pack(out, &packet);
	return length;
}

} // end namespace cell
